package com.github.boluo1230.wechat.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Date;
import java.util.Map;

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
@Document(collection = "wx_event")
public class EventMessage {
    /**
     * 文档地址
     * 地址1： https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453 公众号开发文档->消息管理
     * 地址2： https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/receive.html 小程序文档->客服消息
     */

    @JsonProperty("ToUserName")
    private String toUserName; // 开发者微信号
    @JsonProperty("FromUserName")
    private String fromUserName; // 发送方账号（一个OpenID)
    @JsonProperty("CreateTime")
    private Long createTime; // 消息创建时间
    @JsonProperty("MsgType")
    private String msgType; // 消息类型, event
    @JsonProperty("MsgID")
    private String msgId; // 消息id，64位整型  #非事件消息都有
    @JsonProperty("Content")
    private String content; // 文本消息内容
    @JsonProperty("PicUrl")
    private String picUrl; // 图片链接（由系统生成）
    @JsonProperty("MediaId")
    private String mediaId; // 图片消息媒体id，可以调用多媒体文件下载接口拉取数据。
    @JsonProperty("Event")
    private String event; // 事件类型；        #事件消息都有见，开头注释地址1,2
    @JsonProperty("EventKey")
    private String eventKey; // 事件KEY值
    @JsonProperty("Ticket")
    private String ticket;  // 关注事件独有；二维码的ticket
    @JsonProperty("SessionFrom")
    private String sessionFrom; // 进入会话事件, 小程序
    @JsonProperty("Status")
    private String status;
    @JsonProperty("TotalCount")
    private int totalCount;
    @JsonProperty("FilterCount")
    private int filterCount;
    @JsonProperty("SentCount")
    private int sentCount;
    @JsonProperty("ErrorCount")
    private int errorCount;

    private Date systemDate;

    public EventMessage() {
    }

    public EventMessage(Map<String, String> messageMap) {
        this.toUserName = messageMap.get("ToUserName");
        this.fromUserName = messageMap.get("FromUserName");
        this.createTime = Long.parseLong(messageMap.get("CreateTime"));
        this.msgType = messageMap.get("MsgType");
        this.msgId = messageMap.get("MsgID");
        this.content = messageMap.get("Content");
        this.picUrl = messageMap.get("picUrl");
        this.event = messageMap.get("Event");
        this.eventKey = messageMap.get("EventKey");
        this.ticket = messageMap.get("Ticket");
        this.status = messageMap.get("Status");
        try {
            this.totalCount = StringUtils.isBlank(messageMap.get("TotalCount")) ? 0 : Integer.valueOf(messageMap.get("TotalCount"));
            this.filterCount = StringUtils.isBlank(messageMap.get("FilterCount")) ? 0 : Integer.valueOf(messageMap.get("FilterCount"));
            this.sentCount = StringUtils.isBlank(messageMap.get("SentCount")) ? 0 : Integer.valueOf(messageMap.get("SentCount"));
            this.errorCount = StringUtils.isBlank(messageMap.get("ErrorCount")) ? 0 : Integer.valueOf(messageMap.get("ErrorCount"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.systemDate = new Date();
    }
}
